package com.shishuo.cms.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shishuo.cms.dao.GoodsItemDao;
import com.shishuo.cms.entity.GoodsItemEntity;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.util.ImageUtil;
import com.shishuo.cms.vo.GoodsItemEntityVo;

@Service
public class GoodsItemService extends BaseService<GoodsItemEntity, Long>
{
	private GoodsItemDao goodsItemDao;

	@Autowired
	public void setGoodsItemDao(GoodsItemDao goodsItemDao)
	{
		super.setBaseDao(goodsItemDao);
		this.goodsItemDao = goodsItemDao;
	}
	@Autowired
	private GoodsItemKeywordsService goodsItemKeywordsService;
	
	public PageVo<GoodsItemEntityVo> getAllListPage(int pageNum, int orderNum)
																				throws Exception
	{
		PageVo<GoodsItemEntityVo> pageVo = new PageVo<GoodsItemEntityVo>(
				pageNum);
		List<GoodsItemEntityVo> list = goodsItemDao.getAllListVo(
				pageVo.getOffset(), pageVo.getRows(), 0, orderNum);
		pageVo.setList(list);
		pageVo.setCount(goodsItemDao.getAllListVoCount(0));
		return pageVo;
	}

	/**
	 * 通过类型ID及其当前页获取文章列表
	 * 
	 * @param page
	 * @param id
	 * @throws Exception
	 */
	public PageVo<GoodsItemEntityVo> getAllListPageByClassId(int pageNum,
																long classId, String classAliasName, int orderNum, int pageSize) throws Exception
	{
		PageVo<GoodsItemEntityVo> pageVo = new PageVo<GoodsItemEntityVo>(
				pageNum);
		pageVo.setRows(pageSize);
		List<GoodsItemEntityVo> list = goodsItemDao.getAllListVo(
				pageVo.getOffset(), pageVo.getRows(), classId, orderNum);
		pageVo.setList(list);
		pageVo.setCount(goodsItemDao.getAllListVoCount(classId));
		pageVo.setDirPath("/goods/" + classAliasName + "/");
		return pageVo;
	}

	/**
	 * 搜索获取
	 * 
	 * @param pageNum
	 * @param classId
	 * @param keyWords
	 * @param orderNum
	 * @return
	 * @throws Exception
	 */
	public PageVo<GoodsItemEntityVo> getSearchListPage(int pageNum, String keyWords, int orderNum) throws Exception
	{
		goodsItemKeywordsService.saveKeywords(keyWords);
		PageVo<GoodsItemEntityVo> pageVo = new PageVo<GoodsItemEntityVo>(
				pageNum);
		pageVo.setRows(12);
		List<GoodsItemEntityVo> list = goodsItemDao.getSearchListVo(
				pageVo.getOffset(), pageVo.getRows(), keyWords, orderNum);
		pageVo.setList(list);
		pageVo.setCount(goodsItemDao.getSearchListVoCount(keyWords));
		pageVo.setDirPath("/goods/search/" + keyWords + "/");
		return pageVo;
	}

	public GoodsItemEntityVo getVoById(long goodsId) throws Exception
	{
		return goodsItemDao.getVoById(goodsId);
	}

	/**
	 * 
	 * @param topCount
	 * @param aliasName
	 * @return
	 */
	public List<GoodsItemEntityVo> getTopArticle(int topCount, String aliasName)
	{
		return goodsItemDao.getTopVoByAliasName(topCount, aliasName);
	}

	@Transactional(rollbackFor = java.lang.Exception.class)
	public void importGoods(GoodsItemEntity goodsItemEntity, String filePath)
																				throws Exception
	{
		File file = new File(filePath);
		BufferedReader reader = null;
		try
		{
			System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null)
			{
				// 显示行号
				System.out.println("line " + line + ": " + tempString);
				if (line != 1)
				{
					saveGoods(goodsItemEntity, tempString);
				}
				line++;
			}
			reader.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (reader != null)
			{
				try
				{
					reader.close();
				}
				catch (IOException e1)
				{
				}
			}
		}
	}

	private void saveGoods(GoodsItemEntity goodsItemEntity, String tempString)
																				throws Exception
	{
		// 解析并保存到数据库
		String[] itemsStrings = tempString.split(",");
		if (itemsStrings.length > 10)
		{
			goodsItemEntity.setProductIid(itemsStrings[0]);
			goodsItemEntity.setTitle(itemsStrings[1]);
			goodsItemEntity.setContent(itemsStrings[1]);
			goodsItemEntity.setDescription(itemsStrings[1]);
			goodsItemEntity
					.setProductPrice(Double.parseDouble(itemsStrings[2]));
			goodsItemEntity.setProductPicUrl(itemsStrings[3]);
			goodsItemEntity.setProductSid(itemsStrings[4]);
			goodsItemEntity.setProductSellerName(itemsStrings[5]);
			goodsItemEntity.setProductClickUrl(itemsStrings[6]);
			goodsItemEntity.setProductSellerUrl(itemsStrings[7]);
			goodsItemEntity.setProductCommission(Double
					.parseDouble(itemsStrings[8]));
			goodsItemEntity.setProductSellTotal(Integer
					.parseInt(itemsStrings[9]));
			goodsItemEntity.setProductOriginalPrice(Double
					.parseDouble(itemsStrings[10]));
			goodsItemEntity
					.setProductPromotions(itemsStrings.length == 12 ? itemsStrings[11]
																	: "");
			insert(goodsItemEntity);
		}
	}
	
}
